/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upc.poo;

/**
 *
 * @author heraldo
 */
public class EvaluadorBooleano {

    public boolean evaluar(String expression) {
        boolean eval = true;
        int a = 0, b = 0;
        boolean is_and = false;
        if (expression.indexOf(' ') < 0) {
            System.out.println(expression);
            return expression.equals("verdadero");
        }
        while (b < expression.length()) {
            b = expression.indexOf(" y ", a);
            if (b >= 0) {
                eval = eval && this.evaluar(expression.substring(a, b));
                a = b + 3;
                b = (expression.substring(a).startsWith("v") ? a + 9 : a + 5);
                eval = eval && this.evaluar(expression.substring(a, b));
                is_and = true;
            } else {
                b = expression.indexOf(" o ", a);
                if (b >= 0) {
                    eval = eval || this.evaluar(expression.substring(a, b));
                    a = b + 3;
                    b = (expression.substring(a).startsWith("v") ? a + 9 : a + 5);
                    eval = eval || this.evaluar(expression.substring(a, b));
                    is_and = false;
                } else {
                    a = b + 3;
                    b = expression.length();
                    if (is_and) {
                        eval = eval && this.evaluar(expression.substring(a, b));
                    } else {
                        eval = eval || this.evaluar(expression.substring(a, b));
                    }

                }
            }
        }
        return eval;
    }
}
